package com.hq.common.domain;

import org.apache.commons.lang.StringUtils;
import com.hq.common.domain.SearchValue.*;
import java.util.*;

public class SearchObject {

	private List<SearchValue> condition = new ArrayList<SearchValue>();
	private int pageNum;
	private int pageSize;
	
	public SearchObject() {
	}
	
	public SearchObject(int pageNum, int pageSize) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
	}
	
	public void addCondition(String name, Object value, Pattern pattern){
		condition.add(new SearchValue(name, value, pattern));
	}

	public void addOrderCondition(String name,Order order,int orderSeq){
		condition.add(new SearchValue(name, order, orderSeq));
	}

	public void addCondition(SearchValue searchValue){
		condition.add(searchValue);
	}

	public void addAllCondition(List<SearchValue> searchValueList){
		condition.addAll(searchValueList);
	}
	/**
	 * 获取排序参数
	 * @return
	 */
	public Map<String, Order> getOrder(){
		
		Map<String, Order> map = new LinkedHashMap<String, Order>();
		
		List<SearchValue> list = new ArrayList<SearchValue>(this.condition);
		
		Collections.sort(list, new Comparator<SearchValue>() {

			@Override
			public int compare(SearchValue o1, SearchValue o2) {
				if(o1.getOrderSeq() > o2.getOrderSeq()){
					return 1;
				} else {
					return 0;
				}
			}
		});
		
		for(SearchValue search : list){
			
			if(StringUtils.isBlank(search.getName()) || search.getOrder() == null){
				continue;
			}
			map.put(search.getName(), search.getOrder());
		}
		
		return map;
	}

	public SearchValue getCondition(String name){

		for(SearchValue value : this.condition){

			if(value.getName().equals(name)){
				return value;
			}
		}
		return null;
	}
	
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public List<SearchValue> getCondition() {
		return condition;
	}
	public void setCondition(List<SearchValue> condition) {
		this.condition = condition;
	}
}